package com.woniuxy.lab.woniu_lab.util;



import com.woniuxy.lab.woniu_lab.commons.Shell;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class ShellUtil {

    private Shell shell;

    public ShellUtil() {
    }

    public ShellUtil(String host, String username, String password) {
        this.shell = new Shell(host, username, password);
    }

    public static ShellUtil getShell(String host, String username, String password) {
        return new ShellUtil(host, username, password);
    }

    // ----------------------

    public String run(String cmd) {
        return shell.execCommand(cmd);
    }

    public String cp(String oldFile, String newFile){
        return shell.execCommand("cp "+oldFile+" "+newFile);
    }

    public String mv(String oldFile, String newFile){
        return shell.execCommand("mv "+oldFile+" "+newFile);
    }

    public String ls(String path){
        return shell.execCommand(path);
    }

    public String mkdir(String dir){
        return shell.execCommand("mkdir "+dir);
    }

    public String touch(String file){
        return shell.execCommand("touch "+file);
    }

    public String ps(String keyWord){
        return shell.execCommand("ps aux | grep "+keyWord);
    }

    public String cpr(String oldFile, String newFile){

        return shell.execCommand("cp -r "+oldFile+" "+newFile);
    }
    public String cpry(String oldFile, String newFile){

        return shell.execCommand("cp -ry "+oldFile+" "+newFile);
    }

    /**
     * 启动 docker-compose
     */
    public void startCompose(String path){
        shell.execCommand("cd "+ path +" && docker-compose up -d");
    }

    /**
     * 关闭 docker-compose
     */
    public void stopCompose(String path){
        shell.execCommand("cd "+ path +" && docker-compose down");
    }

    /**
     * 删除用户文件
     */
    public void delUserFolder(String path){
        shell.execCommand("rm -rf "+ path);
    }

    /**
     * 删除用户镜像
     * @param name 镜像名
     */
    public void delUserImage(String name){
        shell.execCommand("docker rmi "+ name);
    }

    /**
     * 解压zip文件到指定路径，并默认覆盖
     * @param unZipFile 解压到的路径
     * @param fileName 被解压的文件名
     * @return 执行结果
     */
    public String unZip(String unZipFile, String fileName){

        return shell.execCommand("unzip -o -j "+fileName+" -d "+unZipFile);
    }

    /**
     * 在用户文件中根据关键字获取文件名
     * @param userFile 用户文件路径
     * @param condition 关键字
     * @return 文件名
     */
    public String getFileNameByCondition(String userFile,String condition){

        return shell.execCommand( "cd "+userFile+" && ls | grep "+condition);
    }



    /**
     * 放开端口
     * @param port 端口号
     * @return 成功?
     */
    public void letGoPort(String port){
        String s = shell.execCommand("firewall-cmd --zone=public --add-port=" + port + "/tcp --permanent");
        //刷新防火墙

        shell.execCommand("firewall-cmd --reload");

    }

    public void closePort(String port){
        shell.execCommand("firewall-cmd --zone=public --remove-port="+port+"/tcp --permanent");
        shell.execCommand("firewall-cmd --reload");
    }

    public String catPorts(String path){
        String s = shell.execCommand("cat "+path+" | grep 'ports:' -C 1 | grep '\"*\"'" );
        return s;
    }

    public String catVolumes(String path){
        String s = shell.execCommand("cat "+path+" | grep -A 1 'volumes:' | grep -v 'volumes:'" );
        return s;
    }


    /**
     * 获取需要修改的端口
     * @param yamlPath docker-compose.yaml的路径
     * @return 所有端口
     */
    public List<String> getYamlPorts(String yamlPath){
        String replace = catPorts(yamlPath).replace("\"", "").replace(" ", "").
                replace("-","").replace("\n",":");;
        String[] split = replace.split(":");
        List<String> ports = new ArrayList<>();
        for (int i = 0; i < split.length; i+=2) {
            ports.add(split[i]);
        }
        for (String port : ports) {
            System.out.println("端口:"+port);
        }
        return ports;
    }

    /**
     * 修改文件内容,使用于yaml
     * @param oldText 需要替换的文字
     * @param newText 新文字
     * @param yamlPath 需要替换的文件路径
     */
    public void updatePort(String oldText,String newText,String yamlPath){
        shell.execCommand("sed -i \"s/"+oldText+":/"+newText+":/g\" "+yamlPath);
    }


    public List<String> getYamlVolumes(String yamlPath){

        String volumesStr =catVolumes(yamlPath);
        String replace = volumesStr.replace("--", "").replace("-", "").
                replace(" ","");

        String[] split = replace.split("\n");
        List<String> volumes = new ArrayList<>();
        for (String s : split) {
            String[] split1 = s.split(":");
            if (split1.length>1){
                volumes.add(split1[0]);
            }
        }
        for (String volume : volumes) {
            System.out.println("卷:"+volume);
        }
        return volumes;
    }
}
